import 'package:flutter/material.dart';
import 'package:slwl/pages/tab_bar/index.dart';
import 'package:slwl/utils/http.dart';
import 'package:slwl/utils/toast.dart';
import 'package:slwl/utils/token.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  //密码可见
  bool _isPasswordVisible = true;
  //账号控制器
  final TextEditingController _accountController = TextEditingController();
  //密码控制器
  final TextEditingController _passwordController = TextEditingController();

  //登录
  Future<void> _login() async {
    String account = _accountController.text;
    String password = _passwordController.text;
    if (account.isEmpty) {
      ToastUtils.showInfo('请输入账号');
      return;
    }
    if (password.isEmpty) {
      ToastUtils.showInfo('请输入密码');
      return;
    }
    try {
      var res = await http.post(
        '/driver/login/account',
        data: {
          'account': account,
          'password': password,
        },
      );

      if (res['code'] == 200) {
        // 登录成功，将 token 保存到本地
        TokenManager tokenManager = TokenManager();
        tokenManager.setToken(res['data']);
        // 登录成功，跳转到首页
        Navigator.pushReplacement(context,
            MaterialPageRoute(builder: (context) => const TabBarPage()));
        ToastUtils.showSuccess('登录成功');
      }
    } catch (e) {
      print(e);
      ToastUtils.showError(e.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: const Text('神领快递'),
        centerTitle: true,
        // 隐藏返回按钮
        automaticallyImplyLeading: false,
        backgroundColor: Colors.white,
      ),
      body: Container(
        padding: const EdgeInsets.all(20),
        child: Column(
          children: [
            Container(
              child: const Row(
                children: [
                  Text(
                    '账号登录',
                    style: TextStyle(
                        fontSize: 24,
                        color: Colors.black,
                        fontWeight: FontWeight.w600),
                  ),
                  Spacer(),
                  Row(
                    children: [
                      Text(
                        '手机号登录',
                        style: TextStyle(
                            fontSize: 16,
                            color: Color(0xFFEF4F3F),
                            fontWeight: FontWeight.w500),
                      ),
                      Icon(
                        Icons.arrow_forward_ios,
                        size: 16,
                        color: Color(0xFFEF4F3F),
                      ),
                    ],
                  ),
                ],
              ),
            ),
            const SizedBox(
              height: 45,
            ),
            //账号输入框
            Container(
              child: Column(
                children: [
                  TextField(
                    controller: _accountController,
                    decoration: const InputDecoration(
                      hintText: '请输入账号',
                      border: InputBorder.none,
                    ),
                  ),
                  Container(
                    height: 1,
                    color: Colors.grey,
                  ),
                  const SizedBox(
                    height: 15,
                  ),
                  //密码输入框
                  TextField(
                    controller: _passwordController,
                    //密码显隐
                    obscureText: _isPasswordVisible, //是否隐藏密码
                    decoration: InputDecoration(
                      hintText: '请输入密码',
                      border: InputBorder.none,
                      suffixIcon: IconButton(
                        icon: Icon(
                          _isPasswordVisible
                              ? Icons.visibility
                              : Icons.visibility_off,
                          color: Colors.grey,
                        ),
                        onPressed: () {
                          setState(() {
                            _isPasswordVisible = !_isPasswordVisible;
                          });
                        },
                      ),
                    ),
                  ),
                  Container(
                    height: 1,
                    color: Colors.grey,
                  ),
                  const SizedBox(
                    height: 15,
                  ),
                  //登录按钮
                  GestureDetector(
                    onTap: _login,
                    child: Container(
                      width: double.infinity,
                      height: 50,
                      margin: const EdgeInsets.only(top: 40),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        color: Color(0xFFEF4F3F),
                        borderRadius: BorderRadius.circular(25),
                      ),
                      child: const Text(
                        '登录',
                        style: TextStyle(
                          fontSize: 16,
                          color: Colors.white,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
